<?php

namespace App\Http\Controllers\Api;
use App\User;
use JWTAuth;
use Auth;
use Illuminate\Http\Request;


class UserController extends BaseController
{
    public $user;

    public function __construct(Request $request)
    {
        parent::__construct();
        $this->user = $this->is_login($request);
    }

    /**
     * @param Request $request
     * 查看用户信息
     */
    public function info(Request $request){

        if (!$this->user['data']->can('user_manager')){
            return error('NoPermission');
        }

        $user_type = $this->user['data']['type'];
        $cinema_code = $this->user['data']['cinema_code'];

        if (!$id = $request->id){
            return error('用户id缺失');
        }

        $user_info = User::where(['type'=>$user_type,'cinema_code'=>$cinema_code,'status'=>1])->find($id);
        if (!$user_info){
            return error('用户不存在');
        }
        $user_roles = \DB::table('role_user')->where(['user_id'=>$id])->pluck('role_id');
        $user_info['role'] = $user_roles;
        return success($user_info,'查询成功');
    }

    /**
     * 查询用户列表
     */
    public function index(){

        if (!$this->user['data']->can('user_manager')){
            return error('NoPermission');
        }

        $user_type = $this->user['data']['type'];
        $cinema_code = $this->user['data']['cinema_code'];
        $user_list = \DB::table('users');
        $list = $user_list->where(['type'=>$user_type,'cinema_code'=>$cinema_code])
            ->where('status','!=',2)
            ->where('is_manager','!=',1)
            ->paginate(15);
        return success($list,'查询成功');
    }

    /**
     * @param Request $request
     * 重置密码
     */
    public function reset_password(Request $request){

        if (!$this->user['data']->can('user_manager')){
            return error('NoPermission');
        }

        $user_type = $this->user['data']['type'];
        $cinema_code = $this->user['data']['cinema_code'];

        if (!$id = $request->id){
            return error('用户id缺失');
        }

        $user_info = User::where(['type'=>$user_type,'cinema_code'=>$cinema_code])->find($id);
        if (!$user_info){
            return error('用户不存在');
        }

        $user_info->password = bcrypt($user_info->account.'1234');
        if ($user_info->save()){
            return success($user_info,'重置密码成功');
        }else{
            return error('重置密码失败');
        }
    }


    /**
     * @param Request $request
     * 修改密码
     */
    public function change_password(Request $request){
        $account = $this->user['data']['account'];
        $user_id = $this->user['data']['id'];

        if (!$old_password = $request->old_password){
            return error('原始密码缺失');
        }

        if (!$new_password = $request->new_password){
            return error('新密码缺失');
        }

        $info = Auth::attempt(['account' => $account, 'password' => $old_password]);
        if (!$info){
            return error('用户密码错误');
        }

        $user = User::find($user_id);
        $user->password = bcrypt($new_password);

        if ($user->save()){
            return success($user,'修改密码成功');
        }else{
            return error('修改密码失败');
        }

    }


    /**
     * 添加用户
     */
    public function add(Request $request){

        if (!$this->user['data']->can('user_manager')){
            return error('NoPermission');
        }

        $user_type = $this->user['data']['type'];
        $cinema_code = $this->user['data']['cinema_code'];
        $chain_code = $this->user['data']['chain_code'];
        $cinema_id = $this->user['data']['cinema_id'];
        if (!$account = $request->account){
            return error('用户名缺失');
        }
        if (!$name = $request->name){
            return error('姓名缺失');
        }
        if (!$sex = $request->sex){
            return error('性别缺失');
        }
        if (!$role_ids = $request->role_ids){
            return error('用户角色必填');
        }
        $count = User::where(['account'=>$account])->count();
        if ($count>0){
            return error('用户名已存在');
        }

        $role_ids = explode(',',$role_ids);
        $password = $request->password;
        $password = $password?bcrypt($password):bcrypt($account.'1234');

        $user = new User();
        $user->account = $account;
        $user->name = $name;
        $user->sex = $sex;
        $user->password = $password;
        $user->position = $request->position;
        $user->department = $request->department;
        $user->phone = $request->phone;
        $user->type = $user_type;
        $user->cinema_code = $cinema_code;
        $user->cinema_id = $cinema_id;
        $user->chain_code = $chain_code;
        \DB::beginTransaction();
        try{
            $res_add_user = $user->save();
            $res_add_role = $user->roles()->sync($role_ids);
            if ($res_add_user&&$res_add_role){
                \DB::commit();
                logs($this->user,'添加用户'.$name.'成功');
                return success($user->id,'添加成功');
            }
        }catch (\Exception $e){
            \DB::rollBack();
            logs($this->user,'添加用户'.$name.'失败');
            return error('添加失败');
        }
    }

    /**
     * 编辑用户
     */
    public function edit(Request $request){

        if (!$this->user['data']->can('user_manager')){
            return error('NoPermission');
        }

        $user_type = $this->user['data']['type'];
        $cinema_code = $this->user['data']['cinema_code'];
        if (!$id=$request->id){
            return error('用户id缺失');
        }
        $user_info = User::where('status','!=',2)
            ->where(['type'=>$user_type,'cinema_code'=>$cinema_code])->find($id);
        if (!$user_info){
            return error('用户不存在');
        }
        if (!$user_info->name = $request->name){
            return error('姓名缺失');
        }
        if (!$user_info->sex = $request->sex){
            return error('性别缺失');
        }
        if (!$role_ids = $request->role_ids){
            return error('用户角色必填');
        }
        $count = User::where(['account'=>$user_info->account])->where('id','!=',$id)->count();
        if ($count>0){
            return error('用户名已存在');
        }

        $role_ids = explode(',',$role_ids);
        $user_info->position = $request->position;
        $user_info->department = $request->department;
        $user_info->phone = $request->phone;
        \DB::beginTransaction();
        try{
            $res_edit_user = $user_info->save();
            $res_delete_role = \DB::table('role_user')->where(['user_id'=>$user_info->id])->delete();
            $res_add_role = $user_info->roles()->sync($role_ids);
            if ($res_edit_user&&$res_delete_role&&$res_add_role){
                \DB::commit();
                logs($this->user,'编辑用户'.$user_info->name.'成功');
                return success($user_info->id,'编辑成功');
            }
        }catch (\Exception $e){
            \DB::rollBack();
            logs($this->user,'编辑用户'.$user_info->name.'失败');
            return error('编辑失败');
        }

    }

    /**
     * 删除、禁用、恢复用户
     */
    public function change_user_status(Request $request){

        if (!$this->user['data']->can('user_manager')){
            return error('NoPermission');
        }

        if (!$id = $request->id){
            return error('用户id缺失');
        }
        $arr = [1,2,3];
        if (!$status=$request->status){
            return error('修改状态缺失');
        }
        if (!in_array($status,$arr)){
            return error('无此状态');
        }
        $user_info = User::where('status','!=',2)->find($id);
        if (!$user_info){
            return error('用户不存在');
        }
        $user_info->status = $status;
        if ($status==2){
            $user_info->detachRoles($user_info->roles);
        }
        if ($user_info->save()){
            logs($this->user,'改变用户'.$user_info->name.'状态成功');
            return success($user_info,'用户状态已修改');
        }else{
            logs($this->user,'改变用户'.$user_info->name.'状态失败');
            return error('用户状态修改失败');
        }
    }

    /**
     * 退出登录
     */
    public function logout(Request $request){
        logs($this->user,'用户'.$this->user['data']['name'].'退出登录');
        return success('','退出登录成功');
    }


}
